package com.woniu.mealsService.controller;



import com.github.pagehelper.PageInfo;
import com.woniu.annotation.RequireRole;
import com.woniu.exception.meals.MealsException;
import com.woniu.mealsService.mapper.FoodTypeMapper;
import com.woniu.mealsService.model.FoodType;
import com.woniu.mealsService.service.FoodTypeService;
import com.woniu.utils.EnumException;
import com.woniu.utils.JwtUtil;
import com.woniu.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.*;
import java.util.List;

/**
 * <p>
 * 菜品类型表 前端控制器
 * </p>
 *
 * @author 吕雨泽
 * @since 2024年12月06日
 */
@RestController
@RequestMapping("/foodType")
@Api(tags = "菜品类型管理")
@Validated
public class FoodTypeController {
    @Autowired
    private FoodTypeService foodTypeService;
    @Autowired
    private FoodTypeMapper foodTypeMapper;
    @ApiOperation(value = "新增菜品类型")
    @ApiImplicitParam(value = "菜品类型名称", name = "foodTypeName")
    @GetMapping("/createFoodType")
    @RequireRole({"商家"})
    public Result createFoodType(@RequestHeader String token,
                                 @Size(min = 1, max = 10, message = "菜品类型名称长度必须在1-10之间") @NotBlank(message = "菜品类型名称不能为空")
                                 @RequestParam(name = "foodTypeName")
                                 String foodTypeName
    ) throws Exception {
        String change_user_id = JwtUtil.parseToken(token).get("userId").toString();
        foodTypeService.createFoodType(change_user_id,foodTypeName);
        return Result.getResult().setCode(200).setMsg("新增菜品类型成功");
    }
    @ApiOperation(value = "查询该店铺菜品类型列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storeId",value = "店铺id"),
            @ApiImplicitParam(name = "pageNum",value = "页码"),
            @ApiImplicitParam(name = "pageSize",value = "每页数量")
    })
    @GetMapping("/searchFoodTypeList")
    @RequireRole({"商家"})
    public Result searchFoodTypeList(@NotBlank(message = "店铺id不能为空")
                                     @RequestParam(name = "storeId")
                                     String storeId,
                                     @NotNull(message = "页码不能为空")
                                     @Positive(message = "页码必须为正整数")
                                     @RequestParam(name = "pageNum")
                                     Integer pageNum,
                                     @NotNull(message = "每页数量不能为空")
                                     @Positive(message = "每页数量必须为正整数")
                                     @RequestParam(name = "pageSize")
                                     Integer pageSize
    ) throws Exception {
        PageInfo<FoodType> foodTypes = foodTypeService.searchFoodTypeList(storeId,pageNum,pageSize);
        return Result.getResult(PageInfo.class).setCode(200).setMsg("查询菜品类型列表成功").setData(foodTypes);
    }
    @GetMapping("/searchType")
    @RequireRole({"商家"})
    public Result searchType(@NotEmpty(message = "店铺id不能为空")@Min(value = 1,message = "店铺id必须为正整数") String storeId) throws Exception{
        List<FoodType> foodTypes = foodTypeMapper.searchFoodTypeList(storeId);
        if (foodTypes == null|| foodTypes.size() == 0){
            throw new MealsException(EnumException.FoodType_NULL_EXCEPTION);
        }
        return Result.getListResult(FoodType.class).setCode(200).setMsg("查询店铺所有类型成功").setData(foodTypes);
    }

    @DeleteMapping("/deleteFoodType")
    @ApiOperation(value = "删除菜品类型")
    @ApiImplicitParam(value = "菜品类型id",name = "foodTypeId")
    @RequireRole({"商家"})
    public Result deleteFoodType(@RequestHeader String token,
                                 @NotNull(message = "菜品类型id不能为空")
                                 @Positive(message = "菜品类型id必须为正整数")
                                 @RequestParam(name = "foodTypeId")
                                 Long foodTypeId
    ) throws Exception {
        String change_user_id = JwtUtil.parseToken(token).get("userId").toString();
        foodTypeService.deleteFoodType(change_user_id,foodTypeId);
        return Result.getResult().setCode(200).setMsg("删除菜品类型成功");
    }
}

